!-------------------------------------------------------------------------------------
subroutine write_bin(dfu, uxMat, uyMat, it, filenum)
!-------------------------------------------------------------------------------------

  use dimensions
  use parameters, only  :  runname, iSingleMode
  use mpi_wrap

  implicit none

  complex, intent(in)    :: dfu(jv, jz, jy, jx, jDir)
  complex, intent(in)    :: uxMat(jz, jy, jx, jDir)
  complex, intent(in)    :: uyMat(jz, jy, jx, jDir)
  integer, intent(in)    :: it
  integer, intent(in)    :: filenum

  integer     :: flag
  character*7 :: file_ext

  file_ext = '0000000'
  write(file_ext,"(i4.4'.'i2.2)") filenum, myid


!  if (myid .eq. 0) then
!     flag = 1;
!  else 
!     call MPI_Recv(flag, 1, MPI_INTEGER8, &
!          myid-1, myid, MPI_COMM_WORLD, mpi_status, mpi_err);
!  end if


  open(91, file= trim(runname)//'.dfu.'//file_ext, &
         form='unformatted', access='stream')
  write(91) dfu
  close(91)

  if ((jx .gt. 1) .or. (iSingleMode) ) then
     open(92, file= trim(runname)//'.ux.'//file_ext, &
          form='unformatted', access='stream')
     write(92) uxMat
     close(92)
  end if

  if (jy .gt. 1) then
     open(92, file= trim(runname)//'.uy.'//file_ext, &
          form='unformatted', access='stream')
     write(92) uyMat
     close(92)
  end if

!  if (myid .ne. npDir-1) then
!     call MPI_Send(flag, 1, MPI_INTEGER8, &
!          myid+1, myid+1, MPI_COMM_WORLD, mpi_err);
!  end if


  if (myid .eq. 0) then

     file_ext = '0000'
     write(file_ext,"(i4.4)") filenum
 
     open(93, file= trim(runname)//'.tag')
     write(93,*) it, filenum 
     close(93)

  end if

 end subroutine write_bin


!-------------------------------------------------------------------------------------
subroutine read_bin(dfu, uxMat, uyMat, it)
!-------------------------------------------------------------------------------------

  use mpi_wrap
  use dimensions
  use parameters, only  :  runname, iSingleMode

  implicit none

  complex, intent(out)    :: dfu(jv, jz, jy, jx, jDir)
  complex, intent(out)       :: uxMat(jz, jy, jx, jDir)
  complex, intent(out)       :: uyMat(jz, jy, jx, jDir)

  integer, intent(out)    :: it
  integer                 :: filenum

  character*7 :: file_ext

  open(97, file= trim(runname)//'.tag')
  read(97, *) it, filenum 
  close(97)

  write(*,*) it, filenum

  file_ext = '0000000'
  write(file_ext,"(i4.4'.'i2.2)") filenum, myid

  open(94, file= trim(runname)//'.dfu.'//file_ext, &
       form='unformatted', access='stream')
  read(94) dfu
  close(94)

  if ((jx .gt. 1) .or. (iSingleMode) ) then
     open(95, file= trim(runname)//'.ux.'//file_ext, &
          form='unformatted', access='stream')
     read(95) uxMat
     close(95)
  end if

  if (jy .gt. 1) then
     open(96, file= trim(runname)//'.uy.'//file_ext, &
          form='unformatted', access='stream')
     read(96) uyMat
     close(96)
  end if


end subroutine read_bin


!-------------------------------------------------------------------------------------
subroutine io_read_bgk(fbgk, dfbgk)
!-------------------------------------------------------------------------------------

  use dimensions
  use parameters, only  :  runname
  use mpi_wrap

  implicit none

  real,    intent(inout)     :: fbgk(jv, jz)
  complex, intent(inout)     :: dfbgk(jv, jz)


  integer                 :: ierr

  write(msg,*) char(10), ' Reading background distribution', char(10)
  call mpi_wrap_msg

  open(90, file= trim(runname)//'.bg', &
       form='unformatted', access='stream')
  read(90) dfbgk
  close(90)

  fbgk = fbgk + dfbgk
  
  write(msg,*) char(10), '     done reading background distribution', char(10)
  call mpi_wrap_msg


end subroutine io_read_bgk



!-------------------------------------------------------------------------------------
